package cn.tedu.csmall.product.controller;

import cn.tedu.csmall.commons.pojo.vo.PageData;
import cn.tedu.csmall.commons.web.JsonResult;
import cn.tedu.csmall.product.pojo.param.AttributeAddNewParam;
import cn.tedu.csmall.product.pojo.vo.AttributeListItemVO;
import cn.tedu.csmall.product.service.AttributeService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Api(tags = "属性管理模块")
@Slf4j
@RequestMapping("/attributes")
@Validated
public class AttributeController {

    @Autowired
    AttributeService attributeService;


    // http://localhost:9180/attributes/add-new
    @PostMapping("/add-new")
    @PreAuthorize("hasAuthority('/pms/product/add-new')")
    @ApiOperation("添加属性")
    @ApiOperationSupport(order = 100)
    public JsonResult addNew(AttributeAddNewParam attributeAddNewParam){
        log.debug("开始处理【添加属性】的请求，参数:{}",attributeAddNewParam);
        attributeService.addNew(attributeAddNewParam);
        return JsonResult.ok();
    }


    // http://localhost:9180/attributes/list-by-template
    @GetMapping("/list-by-template")
    @PreAuthorize("hasAuthority('/pms/product/read')")
    @ApiOperation("根据属性模板查询属性列表")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams(
            @ApiImplicitParam(name = "templateId",value = "属性模板ID",required = true, paramType = "query")
    )
    public JsonResult listByTemplateId(@Range(min = 1,message = "请提交有效的属性模板ID值！") Long templateId){
        log.debug("开始处理【根据属性模板查询属性列表】的请求，参数:{}",templateId);
        PageData<AttributeListItemVO> pageData = attributeService.listByTemplateId(templateId);
        return JsonResult.ok(pageData);
    }



}
